package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsMapper;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class GoodServcieImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * //查询当前库存（可根据商品类别、商品编码或名称搜索）
     *
     * @param page
     * @param rows
     * @param codeOrName
     * @param goodsTypeId
     * @return
     */
    @Override
    public List<Goods> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        List<Goods> list = goodsMapper.listInventory(page, rows, codeOrName, goodsTypeId);
        return list;

    }

    //添加
    @Override
    public void save(Goods goods) {
          goodsMapper.save(goods);
    }

    //修改
    @Override
    public void update(Goods goods) {
        System.out.println(goods);
        //先进行查找
        Goods selectont = goodsMapper.selectont(goods.getGoodsId());
        if (selectont!=null){
            selectont.setGoodsId(goods.getGoodsId());
            selectont.setGoodsCode(goods.getGoodsCode());
            selectont.setGoodsName(goods.getGoodsName());
            selectont.setMinNum(goods.getMinNum());
            selectont.setGoodsModel(goods.getGoodsModel());
            selectont.setGoodsProducer(goods.getGoodsProducer());
            selectont.setPurchasingPrice(goods.getPurchasingPrice());
            selectont.setGoodsUnit(goods.getGoodsUnit());
            selectont.setGoodsTypeId(goods.getGoodsTypeId());
            //进行修改
              goodsMapper.update(selectont);
            System.out.println(selectont);
        }

    }

    //商品删除（要判断商品状态,入库、有进货和销售单据的不能删除）
    @Override
    public String delete(Integer goodsId) {
           //判断商品的状态  0表示初始值,1表示已入库，2表示有进货或销售单据
         Goods goods= goodsMapper.selectont(goodsId);
         //判断不为空
          if ( goods!=null){
              Integer state = goods.getState();
                if (state!=0){
                     return "商品已入库、或进货和销售单据的不能删除此商品";
                }else{
                    //商品在初始化可以删除
                     goodsMapper.delete(goodsId);
                    return null;
                }
          }
        return null;
    }

    @Override
    //无库存商品列表展示（可以根据商品名称或编码查询）
    public List<Goods> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
               return goodsMapper.getNoInventoryQuantity(page,rows,nameOrCode);
    }

    //有库存商品列表展示（可以根据商品名称或编码查询）
    @Override
    public List<Goods> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
           return   goodsMapper.getHasInventoryQuantity(page,rows,nameOrCode);
    }


    //添加库存、修改数量或成本价
    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
          goodsMapper.saveStock(goodsId,inventoryQuantity,purchasingPrice);
    }

    //库存报警  查询所有 当前库存量 小于 库存下限的商品信息
    @Override
    public List<Goods> listAlarm() {
         return goodsMapper.listAlarm();
    }

}
